#include <linux/init.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/completion.h>
#include <linux/delay.h>

static struct completion my_completion;

// 线程函数
static int my_thread_fn(void *data)
{
    // 模拟一些工作
    printk(KERN_INFO "Thread is running. Waiting for completion...\n");
    wait_for_completion(&my_completion);

    printk(KERN_INFO "Thread received completion signal.\n");
    return 0;
}

static int __init my_module_init(void)
{
    struct task_struct *my_thread;
    int thread_data = 0;

    // 初始化 completion 结构体
    init_completion(&my_completion);

    // 创建内核线程
    my_thread = kthread_create(my_thread_fn, &thread_data, "my_thread");
    if (IS_ERR(my_thread)) {
        printk(KERN_ERR "Failed to create kernel thread.\n");
        return PTR_ERR(my_thread);
    }

    // 启动线程
    wake_up_process(my_thread);

    // 模拟主线程的工作
    printk(KERN_INFO "Main thread is running. Simulating work...\n");
    msleep(3000);  // 等待 3 秒

    // 设置完成状态
    printk(KERN_INFO "Main thread is signaling completion.\n");
    complete(&my_completion);

    return 0;
}

static void __exit my_module_exit(void)
{
    printk(KERN_INFO "Module unloaded.\n");
}

module_init(my_module_init);
module_exit(my_module_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple module to demonstrate complete().");



